﻿using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Data;
using ZBJF.Warranty.Common.Enums.InsuranceOrder;
using ZBJF.Warranty.Common.Enums.InsuranceOrder.Data;
using ZBJF.Warranty.Common.Enums.InsuranceProducts;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.InsuranceProducts;
using ZBJF.Warranty.Model.ViewModel.Order;
using ZBJF.Warranty.Model.ViewModel.Sellers;

namespace ZBJF.Warranty.H5.Controllers
{
    /// <summary>
    /// 保险产品订单
    /// </summary>
    public class InsuranceProductController : BaseController
    {
        #region 产品列表页面
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取保险产品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [LoginFilter(IsCheck = false)]
        public ActionResult GetProductCard(SellerInsuranceProductsModel model)
        {
            model.IsCardProduct = true;
            model.IsDraft = false;
            model.SellerId = CurrentUser == null ? model.SellerId : CurrentUser.SellerId;
            model.SellerEmployeeId = CurrentUser == null ? model.SellerEmployeeId : CurrentUser.ID;
            long sellerEmployeeId = 0;
            if (model.SellerEmployeeId.HasValue)
            {
                sellerEmployeeId = model.SellerEmployeeId.Value;
            }
            model.IsAvailable = true;
            var total = 0;
            var sellerInsuranceProductsList = ServiceFactory.SellerInsuranceProductsServiceInstance.GetPagingList(model, out total).AsNoTracking().ToList().Select(o =>
            {
                var sellerInsuranceProductImages = o.SellerInsuranceProductImages.FirstOrDefault(m => m.IsFirstImage);
                var saleQty = ServiceFactory.InsuranceProductOrderServiceInstance.GetSellerInsuranceProductSaleCount(string.Empty, sellerEmployeeId, o.ID);//取已经付款的
                return new
                {
                    o.ID,
                    o.ProductName,
                    o.SaleRetailPrice,
                    Tags = o.SellerInsuranceProductTags.ToList()
                            .Select(t => t.TagName)
                            .Aggregate(string.Empty, (c, d) => c + "," + d)
                            .Trim(','),
                    FirstImagePath = sellerInsuranceProductImages != null ? sellerInsuranceProductImages.FilePath : "/images/share_img.jpg",
                    SellerInsuranceProductSaleCount = saleQty,
                };
            });

            return Json(new
            {
                total,
                isLastPage = model.iDisplayStart + model.iDisplayLength >= total,
                sellerInsuranceProductsList,
            }, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 验证IMEI后跳转到购买基础产品列表
        /// <summary>
        /// 验证IMEI后跳转到购买基础产品列表
        /// </summary>
        /// <returns></returns>
        [LoginFilter(IsCheck = false)]
        public ActionResult ValidateIMEI()
        {
            return View();
        }
        [HttpPost]
        [LoginFilter(IsCheck = false)]
        public ActionResult ValidateIMEI(InsuranceOrderModel model)
        {
            //todo 通过接口验证，并返回品牌、型号、产品价格等信息
            model.ProductAmount = 100;
            model.ProductBrandName = "苹果";
            model.ProductModelName = "IPhone X";
            Session["InsuranceOrderModel"] = model;
            return RedirectToAction("ProductDerectSellingList");
        }
        /// <summary>
        /// 直销的基础产品，购买后生成订单和保单，不生成卡密
        /// </summary>
        /// <returns></returns>
        [LoginFilter(IsCheck = false)]
        public ActionResult ProductDerectSellingList()
        {
            var insuranceOrder = Session["InsuranceOrderModel"] as InsuranceOrderModel;
            return View(insuranceOrder);
        }
        #endregion

        #region 基础产品详情页面
        /// <summary>
        /// 点击打开基础产品详情页面，点击购买，进入投保填写页面
        /// </summary>
        /// <param name="id">自助保险卡产品ID</param>
        /// <returns></returns>
        [LoginFilter(IsCheck = false)]
        [WeiXinLoginFilterAttribute(CheckLogin = true)]
        public ActionResult BuyProduct(long id)
        {
            var product = ServiceFactory.SellerInsuranceProductsServiceInstance.LoadById(id);
            if (product == null)
            {
                TempData["success"] = false;
                TempData["msg"] = "该产品已下架！";
                return RedirectToAction("Index", "Home");
            }
            return View(product);
        }
        #endregion

        #region 卡产品详情页面
        /// <summary>
        /// 产品详情 购买卡产品页面
        /// </summary>
        /// <param name="id">自助保险卡产品ID</param>
        /// <returns></returns>
        [LoginFilter(IsCheck = false)]
        [WeiXinLoginFilterAttribute(CheckLogin = true)]
        public ActionResult ToBeCreateInsuranceOrder(long id)
        {
            var sellerInsuranceProducts = ServiceFactory.SellerInsuranceProductsServiceInstance.LoadById(id);
            if (sellerInsuranceProducts == null)
            {
                TempData["success"] = false;
                TempData["msg"] = "该产品已下架！";
                return RedirectToAction("Index", "Home");
            }
            return View(sellerInsuranceProducts);
        }
        #endregion
    }
}